package com.gs.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.RespectBinding;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gs.pojo.Attribute;
import com.gs.pojo.Passenger;
import com.gs.service.passenger.PassengerService;
import com.gs.utils.Page;

@Controller
@RequestMapping("/Passenger")
public class PassengerController {
	@Resource
	private PassengerService passengerService;
	
	@RequestMapping("/tolist.do")
	public String toPassengerList(Model model,@RequestParam(value="currentPage",required=false,defaultValue="1")Integer currentPage,
			@RequestParam(value="txtname",required=false,defaultValue="")String txtname){
		Page<Passenger> page=new Page<Passenger>();
		page.setCurrentPage(currentPage);
		page.setTotal(passengerService.PassengerCount(txtname));
		List<Passenger> passengers=passengerService.PassengerList(txtname,page.getCurrentPage(),page.getPageSize());
		for (Passenger passenger : passengers) {
			passenger.setGenderName(passengerService.getInformationById(passenger.getGenderID()));
			passenger.setNationName(passengerService.getInformationById(passenger.getNationID()));
			passenger.setPassengerLevelName(passengerService.getInformationById(passenger.getPassengerLevelID()));
			passenger.setPapersName(passengerService.getInformationById(passenger.getPapersID()));
		}
		page.setResult(passengers);
		model.addAttribute("list", page);
		return "passenger/list";
	}
	
	@RequestMapping(value="/toadd.do",method=RequestMethod.GET)
	public String toAdd(Model model){
		Information(model);
		return "passenger/add";
	}
	@RequestMapping(value="/add.do",method=RequestMethod.POST)
	public String doAdd(Passenger passenger){
		passengerService.addPassenger(passenger);
		return "redirect:/Passenger/tolist.do";
	}
	//验证身份证信息
	@RequestMapping(value="/YZ.do")
	@ResponseBody
	public Object YZ(String papersNumber,HttpServletRequest request){
		Integer num=passengerService.YZ(papersNumber);
		return num;
	}
	@RequestMapping("toupdate.do")
	public String toupdate(Integer id,Model model){
		Passenger passenger=passengerService.getPassengerByID(id);
		model.addAttribute("list", passenger);
		Information(model);
		return "passenger/update";
	}
	@RequestMapping("update.do")
	public String doupdate(Passenger passenger){
		passengerService.updatePassenger(passenger);
		return "redirect:/Passenger/tolist.do";
	}
	@RequestMapping("delete.do")
	public String del(Integer id){
		passengerService.delPassengerByID(id);
		return "redirect:/Passenger/tolist.do";
	}
	/**
	 *	属性信息
	 * @param model
	 */
	public void Information(Model model){
		List<Attribute> genders=passengerService.getAttributeByAttributeID(8);//性别
		List<Attribute> nations=passengerService.getAttributeByAttributeID(9);//民族
		List<Attribute> educationDegrees=passengerService.getAttributeByAttributeID(11);//文化程度
		List<Attribute> passengerLevels=passengerService.getAttributeByAttributeID(13);//旅客级别
		List<Attribute> Papers=passengerService.getAttributeByAttributeID(10);//证件类型
		List<Attribute> ThingReasons=passengerService.getAttributeByAttributeID(12);//事由
		model.addAttribute("listGender", genders);
		model.addAttribute("listNation", nations);
		model.addAttribute("listEducationDegree", educationDegrees);
		model.addAttribute("listPassengerLevel", passengerLevels);
		model.addAttribute("listPapers", Papers);
		model.addAttribute("listThingReason", ThingReasons);
	}
}
